// is too difficult to async
import { toStringTypeCheck } from '../../global';
/**
 * 最长递增子序列 单个节点
 * data:[] ,get [deepth]
 */
class longestChildTreeDom2 {
  get [Symbol.toStringTag]() {
    return 'longestChildTree';
  }
  constructor() {
    this['data'] = [];
  }

  get deepth() {
    let largestDeepth = 0;
    let data = this.data;
    if (data.length > 0) {
      for (let i = 0; i < data.length; i++) {
        // console.log(this[data[i]].deepth);
        largestDeepth =
          this[data[i]].deepth + 1 > largestDeepth
            ? this[data[i]].deepth + 1
            : largestDeepth;
      }
    }
    return largestDeepth;
  }
}

function longestChildArr_n2(numArr) {
  //   let count = 0;

  if (!IsNumArr(numArr)) {
    console.log('sorry your sequence is not a Number Array');
    return;
  }

  /**
   * 庄严树
   */
  let venerableTree = new longestChildTreeDom2();

  let venerableTree2 = new longestChildTreeDom2();

  //内部 关键 分割线
  let splitPart = '_';

  /**
   * current Dom --当前节点
   */
  let aTree;

  let mixArr = { data: [] };

  //deep 深度节点 arr
  let DeepthArr;

  function treeShake() {
    for (let i = numArr.length - 1; i >= 0; i--) {
      remindNode(numArr[i]);
      for (let j = i - 1; j >= 0; j--) {
        // count++;
        if (numArr[i] > numArr[j]) {
          if (!toStringTypeCheck(mixArr[numArr[i]], 'array')) {
            mixArr[numArr[i]] = new Array(numArr.length).fill(0);
          }
          mixArr['data'].indexOf(numArr[i]) > -1
            ? undefined
            : mixArr['data'].unshift(numArr[i]);
          //修改 顶点位置的 值 确认 比当前值 小
          mixArr[numArr[i]].splice(j, 1, 1);
        }
      }
    }

    for (let i = mixArr['data'].length - 1; i >= 0; i--) {
      //   console.log(mixArr, mixArr['data'][i], mixArr[mixArr['data'][i]]);
      for (let j = 0; j < mixArr[mixArr['data'][i]].length; j++) {
        // count++;

        if (mixArr[mixArr['data'][i]][j] == 1) {
          //   console.log(
          //     remindNode(numArr[j]),
          //     mixArr['data'][i],
          //     remindNode(mixArr['data'][i])
          //   );
          remindNode(numArr[j])['data'].unshift(mixArr['data'][i]);
          remindNode(numArr[j])[mixArr['data'][i]] = remindNode(
            mixArr['data'][i]
          );
        }
      }
    }
  }

  function remindNode(value) {
    if (venerableTree['data'].indexOf(value) == -1) {
      venerableTree['data'].unshift(value);
      venerableTree[value] = new longestChildTreeDom2();
    }
    return venerableTree[value];
  }

  /**
   * 根据 序号 数组 定位尊严树 中的某个节点 并返回 该节点
   * @param {Array} deepthArr 定位节点 的 数组
   * @returns
   */
  function todeepDom(deepthArr) {
    aTree = venerableTree2;
    for (let i = 0; i < deepthArr.length; i++) {
      if (aTree['data'].indexOf(Number(deepthArr[i])) == -1) {
        return aTree;
      }
      aTree = aTree[deepthArr[i]];
    }
    return aTree;
  }

  /**
   * 根据 节点的 Name 返回 序号数组
   * @param {String} name --节点 Name
   * @returns
   */
  function asyncNameDeepth(name) {
    return name.split(splitPart);
  }

  /**
   * 跟进 节点 获取 对应深度 叶节点
   * @param {longestChildTreeDom} virtualDom 当前递归节点
   * @param {Number} deep2 深度
   */
  function virtualDomSearch(virtualDom, deep2) {
    for (let i = 0; i < virtualDom['data'].length; i++) {
      const el = virtualDom['data'][i];
      if (virtualDom[el].deepth == 0 && deep2 == 1) {
        DeepthArr.push(asyncNameDeepth(virtualDom[el].name));
      } else if (deep2 > 1) {
        virtualDomSearch(virtualDom[el], deep2 - 1);
      }
    }
  }

  /**
   * 给到 最深 节点 或 最深节点 数组
   * @returns
   */
  function getLongestSque() {
    let domD;
    DeepthArr = [];

    virtualDomSearch(venerableTree2, venerableTree2.deepth);
    if (DeepthArr.length > 1) {
      domD = {};
      domD['length'] = DeepthArr.length;
      //   console.log(DeepthArr);
      for (let i = 0; i < DeepthArr.length; i++) {
        domD[i] = todeepDom(DeepthArr[i]);
      }
    } else {
      //   console.log(DeepthArr);
      domD = todeepDom(DeepthArr[0]);
    }

    return domD;
  }

  //   /**
  //    * 给到 最深 节点 或 最深节点 数组
  //    * @returns
  //    */
  //   function getLongestSque() {
  //     let prefixNameArr = [];
  //     let lastDom;
  //     let currentDom;
  //     let deepthL = venerableTree.deepth;
  //     function reDeeper(prefixName, deepth0) {
  //       if (deepth0 < 0) {
  //         prefixNameArr.push(prefixName);
  //         return;
  //       }
  //       console.log(global.structuredClone);
  //       lastDom = global.structuredClone(currentDom);
  //       currentDom = { data: [] };
  //       if (lastDom == undefined) {
  //         for (let i = 0; i < venerableTree['data'].length; i++) {
  //           if (venerableTree[venerableTree['data'][i]].deepth == deepth0) {
  //             currentDom = { data: [] };
  //             currentDom['data'].push(venerableTree['data'][i]);
  //             currentDom[venerableTree['data'][i]] =
  //               venerableTree[venerableTree['data'][i]];

  //             let a =
  //               prefixName == ''
  //                 ? venerableTree['data'][i]
  //                 : prefixName + splitPart + venerableTree['data'][i];

  //             reDeeper(a, deepth0 - 1);
  //           }
  //         }
  //       }
  //     }

  // reDeeper('', deepthL - 1);

  //     return prefixNameArr;
  //   }

  var rebuildVenerableTree = function (virtualArr, prefixName) {
    //初始化
    if (virtualArr == undefined) {
      for (let i = 0; i < venerableTree['data'].length; i++) {
        rebuildVenerableTree([venerableTree['data'][i]]);
      }
    } else {
      //前缀
      let fixName = prefixName == undefined ? '' : prefixName;

      let vt = togetVTsDeep(venerableTree, virtualArr);

      let vt2 = togetVTsDeep(
        venerableTree2,
        virtualArr.slice(0, virtualArr.length - 1)
      );

      vt2['data'].push(virtualArr.slice(-1)[0]);
      vt2[virtualArr.slice(-1)[0]] = new longestChildTreeDom2();
      vt2[virtualArr.slice(-1)[0]]['name'] =
        fixName == ''
          ? virtualArr.slice(-1)[0]
          : fixName + splitPart + virtualArr.slice(-1)[0];

      for (let j = 0; j < vt['data'].length; j++) {
        rebuildVenerableTree(
          [...virtualArr, vt['data'][j]],
          vt2[virtualArr.slice(-1)[0]]['name']
        );
      }
    }
  };

  var togetVTsDeep = function (dom, deepArr) {
    let aTree0 = dom;
    if (deepArr.length == 0) {
      return aTree0;
    }
    for (let i = 0; i < deepArr.length; i++) {
      aTree0 = aTree0[deepArr[i]];
    }
    return aTree0;
  };

  treeShake();

  // console.log(count);
  rebuildVenerableTree();
  return { longestdom: getLongestSque(), venerableTree2 };
  //   return { venerableTree, mixArr };
}

function IsNumArr(numArr) {
  let bool = true;

  let reNum = /^-?\d+(\.\d+)?$/;

  for (let i = 0; i < numArr.length; i++) {
    if (!reNum.test(numArr[i])) {
      bool = false;
    }
  }
  return bool;
}

export { longestChildArr_n2 };
